<template>
  <div class="blog-category-container" v-loading="isLoading">
    <h2>文章分类</h2>
    <RightList :list="list" @select="handleSelect" />
  </div>
</template>

<script>
import { getBlogTypes } from "@/api/blog";
import RightList from "./RightList.vue";
import fetchData from "@/mixins/fetchData";

export default {
  name: "BlogCategory",
  mixins: [fetchData([])],
  components: { RightList },
  data() {
    return {};
  },
  computed: {
    categoryId() {
      return +this.$route.params.categoryId || -1;
    },
    limit() {
      return +this.$route.query.limit || 10;
    },
    list() {
      const result = [
        {
          name: "全部",
          id: -1,
          articleCount: this.data.reduce((a, b) => a + b.articleCount, 0),
        },
        ...this.data,
      ];
      return result.map((v) => ({
        ...v,
        aside: `${v.articleCount}篇`,
        isSelect: v.id === this.categoryId,
      }));
    },
  },
  methods: {
    async fetchData() {
      return await getBlogTypes();
    },
    handleSelect(selectItem) {
      const query = {
        page: 1,
        limit: this.limit,
      };
      if (selectItem.id === -1) {
        this.$router.push({
          name: "Blog",
          query,
        });
      } else {
        this.$router.push({
          name: "CategoryBlog",
          params: {
            categoryId: selectItem.id,
          },
          query,
        });
      }
    },
  },
};
</script>

<style scoped lang="less">
.blog-category-container {
  width: 300px;
  box-sizing: border-box;
  padding: 20px;
  position: relative;
  height: 100%;
  overflow-y: auto;
  h2 {
    font-weight: bold;
    letter-spacing: 2px;
    font-size: 1em;
    margin: 0;
  }
}
</style>
